﻿using AnesSystem.Models;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls.Operation.CQ.CQTL
{
    /// <summary>
    ///麻醉单事件与单次用药
    /// </summary>
    public partial class AnesEventAndMedicineCtrl : UserControl
    {
        private PostoperativeAnalgesiaModel PostoperativeAnalgesia;
        private static AnesEventAndMedicineCtrl _instance;

        public AnesEventAndMedicineCtrl()
        {
            InitializeComponent();
            PostoperativeAnalgesia = DataManager.Instance.CurrentPatientLink.PostoperativeAnalgesia;

            if (PostoperativeAnalgesia.IsDisplay) { 
            TextBlock ztfs = new TextBlock();
            ztfs.Text = "镇痛方式:" + PostoperativeAnalgesia.Ztfs;
            shzt.Children.Add(ztfs);
            TextBlock ccd = new TextBlock();
            if (!string.IsNullOrEmpty(PostoperativeAnalgesia.Ccd1) || !string.IsNullOrEmpty(PostoperativeAnalgesia.Ccd2))
            {
                ccd.Text = "穿刺点:" + PostoperativeAnalgesia.Ccd1 + "与" + PostoperativeAnalgesia.Ccd2;
                shzt.Children.Add(ccd);
            }
            TextBlock Dgsd = new TextBlock();
            if (!string.IsNullOrEmpty(PostoperativeAnalgesia.Dgsd))
            {
                Dgsd.Text = "导管深度:" + PostoperativeAnalgesia.Dgsd + "cm";
                shzt.Children.Add(Dgsd);
            }
            TextBlock Ydzl = new TextBlock();
            Ydzl.Text = "药袋总量:" + PostoperativeAnalgesia.Ydzl + "ml";
            TextBlock Mxsls = new TextBlock();
            Mxsls.Text = "每小时流速:" + PostoperativeAnalgesia.Mxsls + "ml/h";
            TextBlock Pca = new TextBlock();
            Pca.Text = "PCA量:" + PostoperativeAnalgesia.Pca + "ml";
            TextBlock Sdsj = new TextBlock();
            Sdsj.Text = "锁定时间:" + PostoperativeAnalgesia.Sdsj + "min";
            shzt.Children.Add(Ydzl);
            shzt.Children.Add(Mxsls);
            shzt.Children.Add(Pca);
            shzt.Children.Add(Sdsj);
            for (int i = 0; i < PostoperativeAnalgesia.ItemList.Count; i++)
            {
                TextBlock dory = new TextBlock();
                dory.Text = PostoperativeAnalgesia.ItemList[i].Name + ":" + PostoperativeAnalgesia.ItemList[i].Dose + PostoperativeAnalgesia.ItemList[i].Unit;
                shzt.Children.Add(dory);
            }
            }

        }
        

        public static AnesEventAndMedicineCtrl Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AnesEventAndMedicineCtrl();
                }
                return _instance;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            AnesEventAndMedicineCtrlViewModel model = this.DataContext as AnesEventAndMedicineCtrlViewModel;
            if (button.Tag.Equals("addFontSize"))
            {
                model.AddAndMinFontsize(true);
            }
            if (button.Tag.Equals("minusFontSize"))
            {
                model.AddAndMinFontsize(false);
            }
        }

        private void PostoperativeAnalgesia_Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PostoperativeAnalgesiaCtrlViewModel.Instance.PostoperativeAnalgesia=DataManager.Instance.CurrentPatientLink.PostoperativeAnalgesia;
                PostoperativeAnalgesiaCtrlViewModel.Instance.SetPosPatientDrug();
                DrugAndEventWindow.Instance.Title = "术后镇痛";
                //DrugAndEventWindow.Instance.Width = 800;
                //DrugAndEventWindow.Instance.Height = 500;
                DrugAndEventWindow.Instance.CusContent = PostoperativeAnalgesiaCtrl.Instance;
                DrugAndEventWindow.Instance.ShowDialog();
                PostoperativeAnalgesiaCtrl.Instance.Excuted += PostoperativeAnalgesiaCtrl_Excuted;

            }
            catch (Exception ex)
            {
                Common.LogHelper.Error(this.GetType(), ex);
            }
        }

        private void PostoperativeAnalgesiaCtrl_Excuted(object sender, string e)
        {
            List<TextBlock> textBlockList = sender as List<TextBlock>;
            if (e.Equals("Save") && textBlockList != null)
            {
                shzt.Children.Clear();
                foreach (var t in textBlockList)
                {
                    shzt.Children.Add(t);
                }
            }
            if (e.Equals("Delete") && textBlockList != null)
            {
                shzt.Children.Clear();
            }
            if (e.Equals("Cancel") && textBlockList != null)
            {
            }
        }
    }
}
